package com.burpee.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.GroupMemberService;
import com.burpee.service.GroupsService;
import com.burpee.ui.GroupMember;
import com.burpee.ui.Groups;
import com.burpee.ui.Member;


@Controller
@RequestMapping("/group.spring")
public class GroupController {
	@Autowired
	GroupsService groupsService;

	@Autowired
	GroupMemberService groupMemberService;
	//	
	//	@Autowired
	//	ChattingService chattingService;
	@RequestMapping(params="action=grouplist")
	public ModelAndView grouplist(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
		Member member = (Member)request.getSession().getAttribute("login");
		String AddBtn = request.getParameter("AddBtn");
		ModelAndView modelAndView = new ModelAndView();
		if (currentPage == null) {
			currentPage = 1;
		}
		int totalcount = groupsService.countgroups();

		//		List<Groups> groupList = groupsService.list();
		//
		//		modelAndView.addObject("groupList", groupList);
		//		modelAndView.setViewName("group/grouplist");

		StringBuffer stringBuffer = new StringBuffer();
		GeneratePaging generatePaging = new GeneratePaging();
		String url = "action=grouplist";
		String controller = "group";
		
		int onepageCount;
		
		if(member == null) {
			onepageCount = 12;
			
		} else {
			onepageCount = 8;
			
		}
		List<Groups> list = groupsService.list(currentPage,onepageCount);
		
		String paging = generatePaging.paging(currentPage, totalcount, url, onepageCount,".parsingPage",controller);
		if(member !=null)
		{
			Groups groups = groupsService.groupcheck(member.getMemberId());
			if(groups != null) {
				stringBuffer.append("<table class='group-Main-Table' style='height: 190px; width: 900px; margin: 10px 45px;'>");
				stringBuffer.append("<tr style='height: 190px;'>");
				stringBuffer.append("	<td style='width: 30%'></td>");
				stringBuffer.append("	<td background='image/postit.png' style='width: 40%' onclick='javascript:groupPopUp("+groups.getGroupNo()+");'>");
				stringBuffer.append("	   <p class='groupNo .ta'>내 그룹</p>");
				stringBuffer.append(" 	   <p class='groupName .ta'>"+groups.getGroupName() +"</p>");
				stringBuffer.append("	   <p class='groupView .ta'>"+groups.getGroupView() +"</p>");
				stringBuffer.append("	   <p class='groupPoint .ta'>"+groups.getGroupPoint()+"</p>");
				stringBuffer.append("	</td>");
				stringBuffer.append("	<td style='width: 30%'></td>");
				stringBuffer.append("</tr>");
				stringBuffer.append("</table>");
			} else if(AddBtn != null) {
				stringBuffer.append("<form id='groupAddForm' name='groupAddForm' action='group.spring?action=groupadd' method='post'>");
				stringBuffer.append("<table class='group-Main-Table' style='height: 190px; width: 900px; margin: 10px 45px;'>");
				stringBuffer.append("<tr style='height: 190px;'>");
				stringBuffer.append("	<td style='width: 30%'><input type='hidden' name='groupRole' value='G'></td>");
				stringBuffer.append("	<td background='image/postit.png' style='width: 40%'>");
				stringBuffer.append("	   <p class='groupNo .ta' align='center'>그룹 생성</p>");
				stringBuffer.append(" 	   <p class='groupName .ta' align='center'> 그룹명 : <input type='text' name='groupName' size='32'></p>");
				stringBuffer.append("	   <p class='groupView .ta' align='center'><textarea id='groupView' name='groupView' rows='4' style='width: 327px; height: 85px;'></textarea></p>");
				stringBuffer.append("	   <p align='center'><input style='height: 30px; width: 100px;' type='button' value='생성' onclick='javascript:groupAdd();' /></p>");
				stringBuffer.append("	</td>");
				stringBuffer.append("	<td style='width: 30%'></td>");
				stringBuffer.append("</tr>");
				stringBuffer.append("</table>");
				stringBuffer.append("</form>");

			}
		}

		stringBuffer.append("<table class='group-Main-Table' style='height: 400px; width: 900px; margin: 10px 45px;'>");
		stringBuffer.append("	<tr style='height: 24px'>");
		stringBuffer.append("		<td style='width: 23%;'>");
		stringBuffer.append("		</td>");
		stringBuffer.append("		<td colspan='5' align='center'>"+paging);
		stringBuffer.append("		</td>");
		stringBuffer.append("		<td style='width: 23%;' align='right'><input style='height: 30px; width: 100px;' type='button' value='생성' onclick='javascript:groupPreAdd();' />"); 
		stringBuffer.append("		</td>");
		stringBuffer.append("	</tr>");
		int i;
		int pagenow;
		int groupRank= ((currentPage -1) *onepageCount) + 1;
		for(i=0; i<onepageCount;i++) {
			pagenow = ((currentPage-1) * onepageCount) + i +1;
			Groups groups = new Groups();

			if((i+1)%4==1) {
				stringBuffer.append("<tr style='height: 150px;'>");

			}
			if(totalcount >= pagenow) {
				groups = list.get(i);
				//				int groupno = (totalcount-(12*(currentPage-1)))-i;
				stringBuffer.append("<td background='image/postit.png' style='width: 23%;' onclick='javascript:groupPopUp("+groups.getGroupNo()+");'>");
				stringBuffer.append("	<p class='groupNo .ta'>" + groupRank +"</p>");
				stringBuffer.append("	<p class='groupName .ta'>"+ groups.getGroupName() +"</p>");
				stringBuffer.append("	<p class='groupView .ta'>"+ groups.getGroupView() +"</p>");
				stringBuffer.append("	<p class='groupPoint .ta'>건강치:"+ groups.getGroupPoint() +"</p>");
				stringBuffer.append("</td>");
				groupRank++;
			}else {
				stringBuffer.append("<td background='image/postit.png' style='width: 23%;'>");
				stringBuffer.append("	<p></p>");
				stringBuffer.append("	<p></p>");
				stringBuffer.append("	<p></p>");
				stringBuffer.append("</td>");
			}

			if((i+1)%4 !=0) {
				stringBuffer.append("<td style='width: 30px'></td>");
			}
			if((i+1)%4==0) {
				stringBuffer.append("</tr>");
				if((i+1)!=onepageCount){
					stringBuffer.append("<tr style='height: 24px'></tr>");
				}
			}
		}
		stringBuffer.append("</table>");

		modelAndView.addObject("grouplists",stringBuffer);
		modelAndView.addObject("paging", paging);
		modelAndView.setViewName("group/grouplist");

		return modelAndView;

	}
	
	@RequestMapping(params="action=groupadd")
	public ModelAndView groupadd(HttpServletRequest request, HttpServletResponse response, Integer currentPage, Groups groups) {
		Member member = (Member)request.getSession().getAttribute("login");
		groupsService.add(groups, member);
		
		
		ModelAndView modelAndView = new ModelAndView();

		modelAndView.setViewName("redirect:group.spring?action=grouplist");
		
		return modelAndView;
	}
	@RequestMapping(params="action=groupmemberpopup")
	public ModelAndView groupmemberpopup(HttpServletRequest request, HttpServletResponse response) {
		String groupNo = request.getParameter("groupNo");
		
		List<GroupMember> list = groupMemberService.list(Long.parseLong(groupNo));
				
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("groupNo",groupNo);
		modelAndView.addObject("groupMemberList",list);
		modelAndView.setViewName("group/groupMember");
		
		return modelAndView;
	}
	@RequestMapping(params="action=groupmemberadd")
	public ModelAndView groupmemberadd(HttpServletRequest request, HttpServletResponse response, long groupNo) {
		Member member = (Member)request.getSession().getAttribute("login");
		ModelAndView modelAndView = new ModelAndView();
		
		GroupMember groupMember = new GroupMember();
		groupMember.setMemberId(member.getMemberId());
		groupMember.setGroupNo(groupNo);
		groupMember.setGroupMemberRole("M");
		groupMember.setGroupMemberAccept("M");
		
		groupMemberService.add(groupMember);
		List<GroupMember> list = groupMemberService.list(groupNo);
		modelAndView.addObject("groupNo",groupNo);
		modelAndView.addObject("groupMemberList",list);
		modelAndView.setViewName("group/groupMember");
		return modelAndView;
	}
}
